
import { Tools } from "../common/Tools";
import TouchScale from "../item/Common/TouchScale";
import LevelBase from "../LevelBase";
import LvItem from "../LvItem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class TouchMoves extends cc.Component {

    @property({ type: [cc.Node], displayName: "多个触发节点数组" })
    nodes: Array<cc.Node> = [];
    /**是否屏蔽后续触发 */
    @property(cc.Boolean)
    isTouchOther: boolean = true;
    // LIFE-CYCLE CALLBACKS:

    /** 自动移动镜头的方向 */
    autoMove_X: number = 0;
    autoMove_Y: number = 0;

    onLoad() {
        let parent = this.node.parent;
        let fun = () => {
            let levelBase = parent.getComponent(LevelBase);
            if (!levelBase) {
                parent = parent.parent;
                fun();
            } else {
                this._levelBase = levelBase;
            }
        }
        fun();

    }
    protected onDestroy(): void {
        this.offTouchMove();
    }
    private _levelBase: LevelBase = null;

    start() {
        this.onTouchMove();
    }

    onTouchMove() {
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchMoveEndBack, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchMoveEndBack, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveBack, this);
    };
    offTouchMove() {
        this.node.off(cc.Node.EventType.TOUCH_END, this.touchMoveEndBack, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchMoveEndBack, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMoveBack, this);
    };

    touchMoveBack(touches: cc.Event.EventTouch) {
        let node = touches.target;
        let moveScale = this.node.scale / this.getWorldScale(node.scale, node);
        node.position = node.position.add(cc.v3(touches.getDelta().multiplyScalar(moveScale)));

        //@ts-ignore
        let huaNode = this._levelBase.huaNode;
        let tempPos = Tools.getToNodePosForNode(node, huaNode.parent);
        if (Math.abs(tempPos.x) >= huaNode.parent.width / 2 - node.width / 2 - 0) {
            this.autoMove_X = tempPos.x > 0 ? -800 : 800;
        } else if (Math.abs(tempPos.y) >= huaNode.parent.height / 2 - node.height / 2 - 0) {
            this.autoMove_Y = tempPos.y > 0 ? -800 : 800;
        } else {
            this.autoMove_X = 0;
            this.autoMove_Y = 0;
        }
    }

    touchMoveEndBack(touches: cc.Event.EventTouch) {
        this.autoMove_X = 0;
        this.autoMove_Y = 0;
        for (let node of this.nodes) {
            if (!node["isRunTw"]) {
                if (this.isNodeInterNode(this.node, node)) {
                    this._levelBase.runNodeTween(this.node, this._levelBase.getInterTwForName(this.node.name + "_" + node.name).interTween);
                    if (this.isTouchOther) {
                        node["isRunTw"] = true;
                    }
                    return;
                }
            }
        }
        cc.tween(this.node).to(0.1, { position: this.node.getComponent(LvItem).initPos }).start();
    }

    /** 判断两个节点是否相交 */
    isNodeInterNode(node1: cc.Node, node2: cc.Node) {
        if (!node1.active || !node2.active) { return false; };
        let wordPos1 = Tools.getToWorldPosAR(node1);
        let worldScale1 = this.getWorldScale(node1.scale, node1);
        let wordPos2 = Tools.getToWorldPosAR(node2);
        let worldScale2 = this.getWorldScale(node2.scale, node2);
        let size1 = cc.size(node1.width * worldScale1, node1.height * worldScale1);
        let size2 = cc.size(node2.width * worldScale2, node2.height * worldScale2);

        let rect1 = new cc.Rect(wordPos1.x - size1.width / 2, wordPos1.y - size1.height / 2, size1.width, size1.height);
        let rect2 = new cc.Rect(wordPos2.x - size2.width / 2, wordPos2.y - size2.height / 2, size2.width, size2.height);
        if (cc.Intersection.rectRect(rect1, rect2)) {
            return true;
        }
        return false;
    };

    /** 获取世界Scale */
    getWorldScale(tempScale: number = 1, node: cc.Node) {
        if (node.parent) {
            return this.getWorldScale(tempScale * node.parent.scale, node.parent);
        }
        return tempScale;
    };

    update(dt) {
        if (this.autoMove_X == 0 && this.autoMove_Y == 0) {
            return;
        }
        //@ts-ignore
        let huaNode = this._levelBase.huaNode;
        let moveX = this.autoMove_X * dt;
        let moveY = this.autoMove_Y * dt;
        let tempPos = cc.v3(huaNode.position);
        huaNode.getComponent(TouchScale).setResictPos(moveX * huaNode.scaleX, moveY * huaNode.scaleY);

        if (tempPos.x == huaNode.x) {
            moveX = 0;
        } if (tempPos.y == huaNode.y) {
            moveY = 0;
        }
        this.node.position = this.node.position.add(cc.v3(-moveX, -moveY));
    }
}
